#include "primes2.hpp"

namespace __primes2__ {

str *__name__;

static inline list<int> *list_comp_0(list<int> *primos, int i) {
    list<int> *__0;
    int x;
    list<int> *result = new list<int>();

    FOR_IN_SEQ(x,primos,0,1)
        if (((x%i) || (x==i))) {
            result->append(x);
        }
    END_FOR

    return result;
}

void __init() {
    __name__ = new str("__main__");

    primes(100000);
}

list<int> *primes(int N) {
    list<int> *primos;
    int i, index, top;

    if ((N<=3)) {
        return range(2, N);
    }
    primos = __add((new list<int>(1, 2)), range(3, N, 2));
    index = 1;
    top = __int(sqrt(N));

    while(1) {
        i = primos->__getfast__(index);
        if ((i>top)) {
            break;
        }
        index += 1;
        primos = list_comp_0(primos, i);
    }
    return primos;
}

} // module namespace

int main(int argc, char **argv) {
    __shedskin__::__init();
    __primes2__::__init();
}
